package com.xzkydz.all_sensor.socket;

import android.util.Log;

import com.xzkydz.all_sensor.MyApp;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;

/**
 * <p>
 * Socket线程服务类
 * <p>
 */
public class ServerSocketThread extends Thread {

    private Socket socket;
    private String TAG = "asd";
    private MyApp myApp;

    public ServerSocketThread(Socket socket ,MyApp myApp) {
        this.socket = socket;
        this.myApp = myApp;
    }

    @Override
    public void run() {
        InputStream is = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        OutputStream os = null;
        PrintWriter pw = null;
        try {
            // socket获取字节输入流
            is = socket.getInputStream();
            // 将字节输入流转字符输入流
            isr = new InputStreamReader(socket.getInputStream(), "UTF-8");
            // 将字符输入流转行输入流
            br = new BufferedReader(isr);
            String message = null;
            if ((message = br.readLine()) != null) {
                String data = message+socket.getRemoteSocketAddress();
                Log.d("asd", "客户端发来消息：" + data);
                String[] dataArry = data.split("/");
                // 1/index /固定值/精度
                myApp.setIsShowGuding(dataArry[0].equals("0") ? false : true);
                myApp.setAdjust(Float.parseFloat(dataArry[1]));
                myApp.setGudingData(Float.parseFloat(dataArry[2]));
                myApp.setJingduDat(Float.parseFloat(dataArry[3]));

                Log.d(TAG, "向客户端返回的数据==：" + myApp.isIsShowGuding() + " / " + myApp.getAdjust() + " / " + myApp.getGudingData() + " / " + myApp.getJingduDat());

            }
            // 必须先关闭输入流才能获取下面的输出流
            socket.shutdownInput();
            // 向客户端发送现在的调节数据测值
            String msgSend = "" + myApp.isIsShowGuding() + " / " + myApp.getAdjust() + " / " + myApp.getGudingData() + " / " + myApp.getJingduDat();
            /* 服务器向客户端发送数据  */
            OutputStreamWriter osw = null;
            osw = new OutputStreamWriter(socket.getOutputStream(), "UTF-8");
            pw = new PrintWriter(osw, true);
            pw.println(msgSend);
            pw.flush();
            Log.d(TAG, "myApp.getAdjust(): " + myApp.getAdjust());

        } catch (Exception e) {
            e.printStackTrace();
            Log.d(TAG, "run: e.printStackTrace(); ");
        } finally {
            // 关闭输入流
            try {
                socket.shutdownOutput();
            } catch (IOException e) {
                e.printStackTrace();
            }
            // 收工前最后一步：关闭资源
            try {
                if (pw != null) {
                    pw.close();
                }
                if (br != null) {
                    br.close();
                }
                if (isr != null) {
                    isr.close();
                }
                if (os != null) {
                    os.close();
                }
                if (br != null) {
                    br.close();
                }
                if (isr != null) {
                    isr.close();
                }
                if (is != null) {
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
